package com.sanya.upload;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.misc.BASE64Decoder;

public class Base64Pic extends HttpServlet {
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String data = request.getParameter("data");
		String[] split = data.split(",");
		//System.out.println(split[1]);
		data=split[1];

		
		  BASE64Decoder decoder = new BASE64Decoder(); byte[] buffer =
		 decoder.decodeBuffer(data); String path =
		  request.getSession().getServletContext().getRealPath("/upload"); File
		  file = new File(path,System.currentTimeMillis()+".png");
		  System.err.println(file.getPath()); if(!file.exists()) {
		  file.createNewFile(); }
		  
		  FileOutputStream fos = new FileOutputStream(file); fos.write(buffer);
		  fos.flush(); fos.close();
		 

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);

	}

}
